// pages/login/index.js
var utl = require('../../utils/util.js');
var mdk = require('../../utils/md5.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    ischecked: true,
    telephone:'',
    serct:'',
    cantouch:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.getStorage({ 
      key: 'USERNAME',
      success: function(res) {
        console.log('用户名'+res.data);
        var val = res.data;
        that.setData({ 
          telephone: val
        })
      },
    })
    wx.getStorage({
      key: 'PASSW',
      success: function(res) {
        that.setData({
          serct: res.data,
          cantouch:true
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    var ischeck = this.data.ischecked;
    ischeck = !ischeck;
    this.setData({
      ischecked: ischeck
    })
  },
  //登录功能
  loginaction:function(){
    const tele = this.data.telephone;
    const secrt = this.data.serct;
    const ischeck = this.data.ischecked;
    if (tele == '' && secrt==''){
      return;
    }
    var secr = mdk.hexMD5(secrt);
    wx.showLoading({
      title: '登陆中...',
    })
    utl.httServer('/epay_apps/login/loginA0100', { 'loginName': tele, 'passWord': secr,"clientId":'' }, (err, res) => {
      wx.hideLoading();
      if(err){
        wx.showToast({
          title: err,
          icon:'none',
          duration:2000,
        })
        return;
      }
      app.globalData.token = res.response.auth_token;
      app.globalData.userData = res.response.userInfo;
      if (ischeck){
        wx.setStorage({
          key: 'USERNAME',
          data: tele,
        })
        wx.setStorage({
          key: 'PASSW',
          data: secrt,
        })
      }else{
        wx.setStorage({
          key: 'USERNAME',
          data: '',
        })
        wx.setStorage({
          key: 'PASSW',
          data: '',
        })
      }
      wx.reLaunch({
        url: '/pages/index/index?section=' + "1",
        // url:'/pages/regist/userredit'
      })
    })
  
  },
  //手机号
  telephoneAction:function(e){
    this.setData({
      telephone:e.detail.value,
    })
  },
  //密码
  secretAction:function(e){
    this.setData({
      serct:e.detail.value,
      cantouch:true,
    })
  }
  
})